package com.ymt.bpm.web.rest.engine.bpd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PBpdCatetree;
import com.ymt.bpm.service.engine.bpd.BpdCateService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/6.
 */
@RestController
@RequestMapping("/api/v1/engine/bpdTree")
public class BpdCateResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BpdCateService bcs;

    @RequestMapping(value = "/allByTenant", method = RequestMethod.GET)
    public JSONArray allByTenant(HttpServletRequest req,
                                 @RequestParam("timezoneOffset") int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("allByTenant tenantId=" + tenantId);
            log.debug("allByTenant lang=" + lang);
            log.debug("allByTenant timezoneOffset=" + timezoneOffset);
        }
        return bcs.getCateTree(tenantId, lang, timezoneOffset);
    }

    @RequestMapping(value = "/saveCate", method = RequestMethod.POST)
    public JSONObject saveCate(HttpServletRequest req,
                               PBpdCatetree catetree) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("saveCate tenantId=" + tenantId);
            log.debug("saveCate catetree id=" + catetree.getId());
            log.debug("saveCate catetree name=" + catetree.getName());
        }

        return bcs.saveCate(tenantId, loginName, displayName, req.getRemoteAddr(), catetree);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public JSONObject delete(HttpServletRequest req,
                             @RequestParam("id") String id,
                             @RequestParam(name = "tType", required = false) String tType,
                             @RequestParam(name = "cascade", required = false) boolean cascade) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
            log.debug("delete id=" + id);
            log.debug("delete tType=" + tType);
            log.debug("delete cascade=" + cascade);
        }
        return bcs.delete(tenantId, loginName, displayName, req.getRemoteAddr(), id, tType, cascade);
    }

    @RequestMapping(value = "/checkName",
            produces = "plain/text")
    public String checkName(HttpServletRequest req,
                            @RequestParam("parentId") String parentId,
                            @RequestParam("name") String name) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkName tenantId=" + tenantId);
            log.debug("checkName parentId=" + parentId);
            log.debug("checkName name=" + name);
        }
        return bcs.checkName(tenantId, parentId, name);
    }

    @RequestMapping(value = "/addBpdToCate", method = RequestMethod.POST)
    public JSONObject addBpdToCate(HttpServletRequest req,
                                   @RequestParam(name = "check", required = false) boolean check,
                                   PBpdCatetree catetree) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("addBpdToCate tenantId=" + tenantId);
            log.debug("addBpdToCate check=" + check);
            log.debug("addBpdToCate catetree id=" + catetree.getId());
            log.debug("addBpdToCate catetree name=" + catetree.getName());
        }
        return bcs.addBpdToCate(tenantId, check, catetree);
    }

    @GetMapping("getBpdTreeDhx")
    public JSONObject getBpdTreeDhx(HttpServletRequest req) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("getBpdTreeDhx tenantId=" + tenantId);
        }
        return bcs.getBpdTreeDhx(tenantId);
    }

}
